package com.trkj.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.pojo.Emp;
import com.trkj.pojo.Standard;
import com.trkj.service.StandardService;
import com.trkj.service.WorkFlowService;
import com.trkj.vo.StandardVo;

@Controller
@Transactional
@SessionAttributes({"emp", "funcIds"})
public class StandardController {
	@Autowired
	StandardService standardService;
	@Autowired
	WorkFlowService workFlowService;
	
	/**
	 * 离职部署流程接口
	 * @author Lixd
	 */
	@RequestMapping("/bushu_standard")
	public void deploymentProcessDefinition_classpath() {
		workFlowService.deploymentProcessDefinition_classpath("人员转正流程",
				"diagrams/standard_actProcess.png","diagrams/standard_actProcess.bpmn");
	}
	
	/**
	 * 获取转正信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/get_empinfo")
	@ResponseBody
	public StandardVo getEmpInfo(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		return standardService.standardVo(empid);
	}
	
	/**
	 * 填写转正申请表单
	 * @return
	 */
	@RequestMapping("/to_standard")
	public String standard() {
		return "to_standard";
	}
	
	/**
	 * 转正申请暂存
	 * @param standard
	 * @param empId
	 * @param deptId
	 * @return
	 */
	@RequestMapping("/save_standard")
	@ResponseBody
	public boolean save_standard(Standard standard,Integer empId,Integer deptId) {
		if (standardService.isStandard(empId)) {
			Integer isSave = standardService.save_standard(standard, empId, deptId);
			if (isSave != 0) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 转正申请提交
	 * @param standard
	 * @param empId
	 * @param deptId
	 * @return
	 */
	@RequestMapping("/submit_standard")
	@ResponseBody
	public boolean submit_standard(Standard standard,Integer empId,Integer deptId,
			@RequestParam("pk")String processDefinitionKey,Model model) {
		if (standardService.isStandard(empId)) {
			Integer useid = getOnLineEmp(model);
			Integer processId = standardService.save_standard(standard, empId, deptId);
			if (processId != 0) {
				return workFlowService.startEmployProcess(processId, useid, processDefinitionKey);
			}
		}
		return false;
	}
	/**
	 * 获取登录人的id
	 * @author Lixd
	 * @param model
	 * @return
	 */
	private Integer getOnLineEmp(Model model) {
		Emp emp = (Emp)model.asMap().get("emp");
		Integer useid = emp.getEmpId();
		return useid;
	}
}
